package com.nettytime.util;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jayway.jsonpath.JsonPath;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Optional;

/**
 * 测试提取json数据
 */
public class TestJsonpath {

    public static void main(String[] args) {
        getJsonValue();
    }
    private static String getJsonValue() {
        String valueString = StringUtils.EMPTY;
        String bookJson = "{\n" +
                "  \"store\": {\n" +
                "    \"book\":[\n" +
                "      { \"category\": \"reference\",\n" +
                "        \"author\": \"Nigel Rees\",\n" +
                "        \"title\": \"Sayings of the Century\",\n" +
                "        \"price\": 8.95\n" +
                "      },\n" +
                "      { \"category\": \"fiction\",\n" +
                "        \"author\": \"J. R. R. Tolkien\",\n" +
                "        \"title\": \"The Lord of the Rings\",\n" +
                "        \"isbn\": \"0-395-19395-8\",\n" +
                "        \"price\": 22.99\n" +
                "      }\n" +
                "    ],\n" +
                "    \"bicycle\": {\n" +
                "      \"color\": \"red\",\n" +
                "      \"price\": 19.95\n" +
                "    }\n" +
                "  }\n" +
                "}\n";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //输出book节点中某一条数据
            Object reads = JsonPath.read(bookJson, "$.store.book[1].author");
            //输出json节点某一个对象
            Object readss = JsonPath.read(bookJson, "$.store.bicycle.color");
            //输出book节点中所有对象
            Object readsss = JsonPath.read(bookJson, "$.store.book[*]");
            //输出book节点中所有价格小于10的对象
            Object readssss = JsonPath.read(bookJson, "$.store.book[?(@.price<10)]");
            //输出book节点中所有含有isbn的对象
            Object readsssss = JsonPath.read(bookJson, "$.store.book[?(@.isbn)]");
            if(null!=reads){
                stringBuilder.append("输出book节点中某一条数据："+reads.toString()).append("\n");
            }
            Optional<Object> readss1 = Optional.ofNullable(readss);
            if(readss1.isPresent()){
                stringBuilder.append("输出json节点某一个对象："+readss1.get().toString()).append("\n");
            }
            Optional<Object> readsss1 = Optional.ofNullable(readsss);
            if(readsss1.isPresent()){
                if(readsss1.get() instanceof List){
                    System.out.println(JSONObject.toJSONString(readsss1.get(), SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
                }
                stringBuilder.append("输出book节点中所有对象："+readsss1.get().toString()).append("\n");
            }
            Optional<Object> readsss1s = Optional.ofNullable(readssss);
            if(readsss1s.isPresent()){
                stringBuilder.append("输出book节点中所有价格小于10的对象："+readsss1s.get().toString()).append("\n");
            }
            Optional<Object> readsss1ss = Optional.ofNullable(readsssss);
            if(readsss1ss.isPresent()){
                stringBuilder.append("输出book节点中所有含有isbn的对象："+readsss1ss.get().toString()).append("\n");
            }
            if(null!=stringBuilder){
                valueString = stringBuilder.toString();
            }
        }catch (Exception ex){
            System.out.println("解析数据失败");
        }
        return valueString;
    }

}
